package gold.digger;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC419 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {

        int m = 0, n = 0;

        public int countBattleships(char[][] board) {
            m = board.length;
            n = board[0].length;
            int count = 0;
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    if (board[i][j] == 'X') {
                        ++count;
                        dfs(board, i, j);
                    }
                }
            }

            return count;
        }

        public void dfs(char[][] board, int x, int y) {
            if (board[x][y] == '.') return;
            board[x][y] = '.';
            if (x + 1 < m) dfs(board, x + 1, y);
            if (x - 1 >= 0) dfs(board, x - 1, y);
            if (y + 1 < n) dfs(board, x, y + 1);
            if (y - 1 >= 0) dfs(board, x, y - 1);
        }

    }

    public void run() {
        Solution solution = new Solution();
        char[][] arr = {{'X', '.', 'X'}, {'.', '.', 'X'}, {'.', '.', 'X'}};
        System.out.println(solution.countBattleships(arr));
    }

    public static void main(String[] args) throws Exception {
        LC419 an = new LC419();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
